# -*- coding: utf-8 -*-


"""
状态模式
模式特点：当一个对象的内在状态改变时允许改变其行为，这个对象看起来像是改变了其类。

程序实例：描述一个程序员的工作状态，当需要改变状态时发生改变，不同状态下的方法实现不同

代码特点：无
"""


class State(object):
    def write_program(self):
        pass


class Work(object):

    def __init__(self):
        self.hour = 9
        self.current = ForenoonState()

    def set_state(self, temp):
        self.current = temp

    def write_program(self):
        self.current.write_program(self)


class NoonState(State):
    def write_program(self, w):
        print "noon working"
        if w.hour < 13:
            print "fun."
        else:
            print "need to rest."


class ForenoonState(State):
    def write_program(self, w):
        if w.hour < 12:
            print "morning working"
            print "energetic"
        else:
            w.set_state(NoonState())
            w.write_program()


if __name__ == "__main__":
    mywork = Work()
    mywork.hour = 9
    mywork.write_program()
    mywork.hour = 14
    mywork.write_program()